package com.xysq.fragment;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.rockcent.action.CallbackListener;
import com.rockcent.model.CouponModel;
import com.xysq.XYApplication;
import com.xysq.activity.CommodityDetailsActivity;
import com.xysq.adapter.BaseDataAdapter;
import com.xysq.lemon.R;
import com.xysq.util.PropertiesUtil;
import com.xysq.util.ToastUtil;
import com.xysq.util.UseUtil;
import com.xysq.util.UserInfoKeeper;
import com.xysq.widget.LoadData;
import com.xysq.widget.ScrollListener;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Steward_Han on 2015/7/22.
 */
public class OutOfDateTicketsFragment extends BaseFragment implements View.OnClickListener {

    private String loginName;
    private String keyWords = "";
    private int state = 7;
    public static final int type = 0;
    private MyTicketsAdapter myTicketsAdapter;
    ScrollListener scrollListener;

    @InjectView(R.id.swipe)
    SwipeRefreshLayout swipeRefreshLayout;
    @InjectView(R.id.list_data)
    ListView dataList;
    @InjectView(R.id.progress_loading)
    ProgressBar progressBar;
    @InjectView(R.id.txt_empty)
    TextView emptyTxt;

    public static OutOfDateTicketsFragment newInstance(int position) {
        OutOfDateTicketsFragment fragment = new OutOfDateTicketsFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflaterView(inflater, R.layout.activity_out_of_date_tickets, null);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
//        getData();
    }

    private void initView() {
        loginName = UserInfoKeeper.readPhoneNumber(getActivity());
        myTicketsAdapter = new MyTicketsAdapter(getActivity());
        dataList.setAdapter(myTicketsAdapter);
        scrollListener = new ScrollListener(dataList, true, new LoadData() {
            @Override
            public void loadMyData() {
                getData();
            }
        });
        dataList.setOnScrollListener(scrollListener);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                scrollListener.setCurrentPage(1);
                myTicketsAdapter.clearItems();
                getData();
            }
        });
    }

    private void getData() {

        progressBar.setVisibility(View.VISIBLE);
        appAction.listCouponWallet(loginName, keyWords, state, type, scrollListener.getCurrentPage(), PAGE_SIZE, new CallbackListener<List<CouponModel>>() {
            @Override
            public void onSuccess(List<CouponModel> data) {
                swipeRefreshLayout.setRefreshing(false);
                progressBar.setVisibility(View.GONE);
                if (scrollListener.getCurrentPage() == 1) {
                    if (data.isEmpty()) {
                        emptyTxt.setVisibility(View.VISIBLE);
                    } else {
                        emptyTxt.setVisibility(View.GONE);
                        myTicketsAdapter.setItems(data);
                    }
                } else {
                    if(data.isEmpty()){
                        scrollListener.setLoadFinish(false);
                    }else {
                        scrollListener.setLoadFinish(true);
                        emptyTxt.setVisibility(View.GONE);
                        myTicketsAdapter.addItems(data);
                    }
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {
                swipeRefreshLayout.setRefreshing(false);
                progressBar.setVisibility(View.GONE);
                scrollListener.setLoadFinish(true);
            }
        });
    }

    @Override
    public void onClick(View view) {

    }

    public class MyTicketsAdapter extends BaseDataAdapter<CouponModel> {
        private boolean mIsNeedReload = false;
        private Context mContext;

        public MyTicketsAdapter(Context context) {
            super(context);
            mContext = context;
        }

        public boolean isNeedReload() {
            return mIsNeedReload;
        }

        public void setNeedReload(boolean isNeedReload) {
            mIsNeedReload = isNeedReload;
        }

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            final ViewHolder holder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_my_tickets_out_of_date, viewGroup, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            final CouponModel coupon = itemList.get(position);
            String logoUrl = coupon.getPicUrl().split(";")[0];
            if (!TextUtils.isEmpty(logoUrl)) {
                logoUrl = PropertiesUtil.getImageServer() + logoUrl;
                XYApplication.getImageLoader().displayImage(logoUrl, holder.picIv);
            }
            holder.nameTxt.setText(coupon.getName());
            String time = UseUtil.timeToDetailDate(coupon.getUseEndTime());
            holder.timeTxt.setText(time);
            holder.numTxt.setText(String.valueOf(coupon.getCount()));
            holder.nameOtherTxt.setText(coupon.getIntroduce());
            holder.jumpClickAreaLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getActivity(), CommodityDetailsActivity.class);
                    intent.putExtra(CommodityDetailsActivity.COUPONMODEL_ID, coupon.getId());
                    startActivity(intent);
                }
            });
            return convertView;
        }

        class ViewHolder {

            @InjectView(R.id.layout_jump_click_area)
            LinearLayout jumpClickAreaLayout;
            @InjectView(R.id.iv_pic)
            ImageView picIv;
            @InjectView(R.id.txt_customer_name)
            TextView nameTxt;
            @InjectView(R.id.txt_other_name)
            TextView nameOtherTxt;
            @InjectView(R.id.txt_num)
            TextView numTxt;
            @InjectView(R.id.txt_time)
            TextView timeTxt;

            public ViewHolder(View view) {
                ButterKnife.inject(this, view);
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        scrollListener.setCurrentPage(1);
        myTicketsAdapter.clearItems();
        getData();
    }
}
